<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
</head>
<style>
    .app{
        border: 1px solid pink;
        background-color: pink;
        width: 500px;
        margin: 0 auto;
        padding: 10px;
    }
    .header-right{
        transform: translateY(50%);
        height: 50px;
        margin-left: 20px;
    }
</style>
<body>
    <div class="app">
        <div class="header-left">
            <label for="shop">
                商品
                <input type="text" id="shop" v-model="shop">
            </label>
            <br>
            <label for="count">
                数量
                <input type="text" id="count" v-model="count">
            </label>
            <br>
            <label for="price">
                价格
                <input type="text" id="price" v-model="price">
            </label>
        </div>
        <div class="header-right">
            <button @click="updata">添加到购物车</button>
        </div>
        <ul>
            <li v-for="(v,i) in shoplist" :key="i"><font color="red">{{v.name}}</font>的数量是<font color="red">{{v.count}}</font>件,小计<font color="red">{{v.count * v.price}}元</font></li>
        </ul>
        <h3>总计{{countNumber}}件商品,总价{{countMomey.toFixed(2)}}元</h3>
    </div>
        <script>
            new Vue({
                el:'.app',
                data(){
                    return {
                        shoplist:[
                        {
                            name:'牙刷',
                            count:12,
                            price:222
                        }
                        ],
                        shop:'',
                        count:'',
                        price:''
                    }
                },
                methods:{
                    updata(){
                        if(!this.shop || !this.count || !this.price)return
                        this.shoplist.push({
                            name:this.shop,
                            count:this.count,
                            price:this.price
                        })
                        this.shop = ''
                        this.count = ''
                        this.price = ''
                    }
                },
                computed:{
                    countNumber(){
                        return this.shoplist.reduce((sum,item)=>{
                            return sum+Number(item.count);
                        },0);
                    },
                    countMomey(){
                        return this.shoplist.reduce((sum,item)=>{
                            return sum+(item.price*item.count);
                        },0);
                    }
                }
            })
        </script>
    </div>
</body>
</html>